package com.junyidao.game.util;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/31.
 */
public class HttpUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);


    public static Map<String, String> sendGet(String url) {
        Map<String, String> map = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                map = JSON.parseObject(message, HashMap.class);
                String errmsg = map.get("errmsg");
                if (StringUtils.isEmpty(errmsg) || !errmsg.equals("ok")) {
                    map = null;
                    logger.info("调用接口失败 " + url);
                }
            } else {
                logger.info("调用接口失败 " + url);
            }
        } catch (Exception e) {
            logger.info("调用接口失败 " + url);
            e.printStackTrace();
        }

        return map;
    }

    public static Map<String, String> sendStringPost(String url, String pars) {
        Map<String, String> map = null;
        try {
            StringEntity entity = new StringEntity(pars, "utf-8");
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            post.setEntity(entity);
            HttpResponse response = client.execute(post);

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                map = JSON.parseObject(message, HashMap.class);
                String errmsg = map.get("errmsg");
                if (StringUtils.isEmpty(errmsg) || !errmsg.equals("ok")) {
                    map = null;
                    logger.info("调用接口失败 " + url);
                }
            } else {
                logger.info("调用接口失败 " + url);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("调用接口失败 " + url);
        }
        return map;
    }

    public static Map<String, Object> sendStringPost2(String url, String pars) {
        Map<String, Object> map = null;
        try {
            StringEntity entity = new StringEntity(pars, "utf-8");
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            post.setEntity(entity);
            HttpResponse response = client.execute(post);

            HttpEntity resEntity = response.getEntity();
            String message = EntityUtils.toString(resEntity, "utf-8");
            map = JSON.parseObject(message, HashMap.class);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("调用接口失败 " + url);
        }
        return map;
    }

    public static Map<String, String> sendStringPostBackXml(String url, String pars) {
        Map<String, String> map = null;
        try {
            StringEntity entity = new StringEntity(pars, "utf-8");
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            post.setEntity(entity);
            HttpResponse response = client.execute(post);

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                map = parseToXml(message);
            } else {
                logger.info("调用接口失败 " + url);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("调用接口失败 " + url);
        }
        return map;
    }

    public static Map<String, String> parseToXml(String str) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            Document document = DocumentHelper.parseText(str);
            // 得到xml根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList) {
                map.put(e.getName(), e.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
            map = null;
        }
        return map;
    }
}
